package com.emprestimoz;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class EmprestimoZActivity extends Activity {

	public static final String TABLE = "item";
	public static final int VERSION = 1;
	public static final String EMPRESTIMO = "Emprestimo";
	public static final int OBTER_FOTO = 1;
	public static final int AGENDA = 2;
	private ImageView img;
	EditText edit  ;
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == OBTER_FOTO) {
			File file = new File(Environment.getExternalStorageDirectory(), "foto.jpg");
			Uri uri = Uri.fromFile(file);
			InputStream i = null;
			try {
				i = getContentResolver().openInputStream(uri);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
			img.setImageBitmap(BitmapFactory.decodeStream(i));
			
			
		} else if(requestCode == AGENDA){
			Uri contact = data.getData();
			Cursor cursor = managedQuery(contact, null, null, null, null);
			if(cursor.moveToFirst()){
				String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
				edit.setText(name);
			}
			
			
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		img = (ImageView) findViewById(R.id.imageView1);
		edit  = (EditText)  findViewById(R.id.idContato);
		Button button_contatos = (Button) findViewById(R.id.button1);
		button_contatos.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
				startActivityForResult(intent, AGENDA);
			}
		});
		Button button_camera = (Button) findViewById(R.id.botao_camera);
		button_camera.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				File file = new File(Environment.getExternalStorageDirectory(), "foto.jpg");
				Uri uri = Uri.fromFile(file);
				intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
				startActivityForResult(intent, OBTER_FOTO);
			}

		});

		if (getIntent() != null && getIntent().getExtras() != null && getIntent().getExtras().get("idItem") != null) {

			DBHelper db = new DBHelper(this, EmprestimoZActivity.EMPRESTIMO,
					null, EmprestimoZActivity.VERSION);
			StringBuffer query = new StringBuffer();
			query.append("SELECT * FROM item WHERE _id = ?;");
			// db.getWritableDatabase().query(EmprestimoZActivity.TABLE, null,
			// null, null, null, null, null);
			Cursor items = db.getWritableDatabase().rawQuery(
					query.toString(),
					new String[] { getIntent().getExtras().get("idItem")
							.toString() });

			if (items.moveToFirst()) {
				while (items.moveToNext()) {
					items.getString(items.getColumnIndex("item"));
				}
			}

			Button b = (Button) findViewById(R.id.btGravar);
			b.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					EditText t = (EditText) findViewById(R.id.editItem);
					DBHelper db = new DBHelper(EmprestimoZActivity.this,
							EMPRESTIMO, null, VERSION);
					ContentValues values = new ContentValues();
					values.put("Item", t.getText().toString());
					db.getWritableDatabase().insert(TABLE, null, values);

					finish();
				}
			});
		}
	}

}